
<h2 style='text-align:center'>砍价商品管理</h2>

<div style='margin:5px 0;display:flex;height:26px;border:0px solid'>
	<div style='flex:1'>
		<input id='FreeCon_SearchInp' placeholder='搜索标题' type="text" style='border-radius:5px;width:100%;height:100%' />
	</div>
	<div id='FreeCon_SearchBtn' style='border-radius:5px;width:40px;height:100%;box-sizing:border-box;padding:3px 10px;background:#eee'><img src='../Public/Image/search.png' style='height:20px' alt="" /></div>
</div>

<div>
	<select style='width:100%' id="FreeCon_SelType" >
		<option value="all">全部</option>
		<option value="1">正常</option>
		<option value="2">已下架</option>
		<option value="3">已封禁</option>
	</select>
</div>

<div id='FreeCon_GoodsListArea'></div>

<div id='FreeCon_Fpage'></div>







<script>
var controFree_freeList;		//砍价商品列表容器

var controFree_pnow = 1;
var controFree_pMax = 2;
var controFree_psize = 3;		//每页条数
function _controFreeReset(){
	controFree_pnow = 1;
	controFree_pMax = 2;
}


_controFreeInit();


//type 有值为清空容器数据
function _controFreeInit(type){
	if(type){
		controFree_freeList = '';
	}
	if(controFree_freeList){
		C('freeList from val');
		_parseFreeList(controFree_freeList);
	}else{
		C('freeList from after');
		_getFreeListForStoreId(function(){
			_parseFreeList(controFree_freeList);
		});
	}
}


//点击搜索
$('#FreeCon_SearchBtn').click(function(){
	_controFreeReset();
	_controFreeInit();
});



//分类切换
$('#FreeCon_SelType').change(function(){
	_controFreeReset();
	_controFreeInit();
});



function _parseFreeList(freeList){
	var controFree_sType = $('#FreeCon_SelType').find('option:selected').val();
	var controFree__Search = $('#FreeCon_SearchInp').val().trim();

	var info_sType = [];
	//筛选类型
	if(controFree_sType == 'all'){
		info_sType = freeList;
	}else{
		for(var i=0;i<freeList.length;i++){
			if(freeList[i].free_stat == controFree_sType){
				info_sType.push(freeList[i]);
			}
		}
	}
	//筛选搜索值 
	var info_search = [];
	if(controFree__Search){
		for(var i=0;i<info_sType.length;i++){
			if(info_sType[i].title.indexOf(controFree__Search) >= 0){
				info_search.push(info_sType[i]);
			}
		}
	}else{
		info_search = info_sType;
	}
	var info = info_search;
	if(info.length < 1){	//没有数据
		if(controFree_pnow == 1){	//首页
			var str = '<div style="margin:30px auto;text-align:center;color:red">暂无数据！</div>';
			$('#FreeCon_GoodsListArea').html(str);
			$('#FreeCon_Fpage').html('');
		}
	}else{

		var start = (controFree_pnow - 1) * controFree_psize;
		var end = start + controFree_psize;


		//判断最大条数
		if(end >= info.length){	
			end = info.length;
		}

		var str = '';
		for(var i=start;i<end;i++){
			str += "<div style='display:flex;border-bottom:7px solid #fee;font-size:13px;margin:15px 0;'>";
			str += "<div style='border:1px solid #eef;width:100px;height:100px'>";
			if(info[i].banner){
				var img_addr = upyun_shop_img+info[i].banner;
			}else{
				var img_addr = '../Public/Image/base.png';
			}
			str += "<img src='"+img_addr+"' style='width:100%;height:100%' onerror=this.src='../Public/Image/base.png' alt='呵呵了' ></div>";
			str += "<div style='flex:1;padding:0 5px;box-sizing:border-box;color:#999'>";
			//标题
			str += "<div style='font-size:14px;height:30px;word-break:break-all;overflow:hidden;color:#333'>标题："+info[i].title+"</div>";		
			str += "<div style='height:20px;display:flex;margin:8px 0'>";
			//价格
			str += "<div style='flex:1;color:red;font-weight:bold'>价格："+info[i].price+"</div>";
			str += "</div>";

					//商品详情
			str += "<div style='display:flex;width:330px;border:0px solid;margin:8px 0'><div style='width:40px;'>详情：</div><div class='freeGoodsInfo' style='flex:1;overflow:hidden;white-space:nowrap;word-break:break-all'>"+info[i].info+"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa要a</div></div>";

			str += "<div style='height:20px;display:flex'>";
			//状态
			switch(info[i].free_stat){
				case 1:case '1':
					var stat_str = '<text style="color:blue">正常</text>';
					var stat_view = '下架';
					break;
				case 2:case '2':
					var stat_str = '已下架';
					var stat_view = '上架';
					break;
				case 3:case '3':var stat_str = '<text style="color:red">已封禁</text>';break;
				default :
					var stat_str = '审核中';break;
			}
			str += "<div style=''>状态：<span>"+stat_str+"</span></div>";

			str += "<div style='flex:1;text-align:right'>";

			if(info[i].free_stat != 3){
				str += "<span style='padding:1px 7px;background:#aaf;color:#fff;border-radius:5px' onclick=_FreeCon_FreeGoodsDown("+info[i].free_id+","+info[i].free_stat+")>"+stat_view+"</span>　";
			}
			str += "<span style='padding:1px 7px;background:#aaf;color:#fff;border-radius:5px' onclick=FreeCon_FreeGoodsDel("+info[i].free_id+")>删除</span>　";

			str += "<span style='padding:1px 7px;background:#aaf;color:#fff;border-radius:5px' 	onclick=FreeCon_FreeGoodsOrders("+info[i].free_id+")>查看订单</span>　";
			str += "<span style='padding:1px 7px;background:#aaf;color:#fff;border-radius:5px' 	onclick=_addFreeOrder("+info[i].free_id+","+info[i].price+","+info[i].store_id+")>临时</span>　";

			str += "</div></div></div></div>";
		}
		$('#FreeCon_GoodsListArea').html(str);

		var pnum = Math.ceil(info.length/controFree_psize);	//计算最大页
		if(controFree_pnow >= pnum){
			controFree_pnow = pnum;
			controFree_pMax = pnum;	
		}

		Fpage(controFree_pnow,controFree_pMax,function(p){
			controFree_pnow = p;
			controFree_pMax = p + 1;
			_controFreeInit();
		},1,1,'#FreeCon_Fpage')


		//展示或隐藏商品信息
		$('.freeGoodsInfo').click(function(){
			var space = $(this).css('white-space');
			if(space == 'normal'){
				$(this).css('white-space','nowrap');
			}else{
				$(this).css('white-space','normal');
			}
		})
	}

}



//删除砍价商品
function FreeCon_FreeGoodsDel(free_goods_id){
	var data = {
		type:'freeGoodsDel',
		free_goods_id:free_goods_id
	}
	AjaxStore(data,function(res){
		var tmp = _checkJsonFormat(res);
		if(tmp.stat == 'ok'){
			_controFreeInit(1);
			A('操作成功！');
		}else{
			A('有未完成的订单，请先下架商品！');
		}
	});
}


//查看砍价商品下的订单列表
function FreeCon_FreeGoodsOrders(free_id){
	sessionStorage.store_formFreeGoodsId = free_id;	//标记是从砍价商品页跳转到订单页

	//跳转到砍价订单页
	sessionStorage.store_mainCurrentNavPage = 'controlFreeOrder';
	$('#Index_Nav').val('controlFreeOrder');
	_loadPage(1);
}




//临时创建订单
function _addFreeOrder(free_id,price,store_id){
	if(!free_id || !price || !store_id){
		A('缺少参数 ！');return;
	}
	var data = {
		type:'createFreeOrder',
		free_id:free_id,
		price:price,
		store_id:store_id
	}
	AjaxStore(data,function(res){
		C(res);
	})
}


//通过商户ID获取砍价商品列表
function _getFreeListForStoreId(fun){
	var data = {
		type:'getFreeListForStoreId',
		store_id:accountInfo.id
	}
	AjaxStore(data,function(res){
		var tmp = _checkJsonFormat(res);
		controFree_freeList = tmp.data;
		fun()
	})
}


//砍价商品的上下架
function _FreeCon_FreeGoodsDown(free_id,free_stat){
	if(!confirm('确认操作？')){
		return;
	}
	var data = {
		type:'changeFreeGoodsStat',
		free_id:free_id,
		free_stat:free_stat
	}
	AjaxStore(data,function(res){
		var tmp = _checkJsonFormat(res);
		if(tmp.stat == 'ok'){
			A('操作成功！');
			_controFreeInit(1);
		}else{
			A('操作异常！');
		}
	})
}



</script>